package com.yicong.ants.ui.coin;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.cchao.simplelib.ui.adapter.StatefulBindQuickAdapter;
import com.kennyc.view.MultiStateView;
import com.yicong.ants.BR;
import com.yicong.ants.R;
import com.yicong.ants.bean.coin.CoinRecord;
import com.yicong.ants.databinding.RecyclerBinding;
import com.yicong.ants.manager.AdapterHelper;
import com.yicong.ants.manager.http.RetrofitHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 贡献值明细
 *
 * @author cch
 * @version 2020/7/15
 */
public class ContributionRecordActivity extends BaseTitleBarActivity<RecyclerBinding> implements View.OnClickListener {
    StatefulBindQuickAdapter<CoinRecord> mAdapter;

    @Override
    protected int getLayout() {
        return R.layout.recycler;
    }

    @Override
    protected void initEventAndData() {
        setTitleText("贡献值明细");
        initAdapter();
        onLoadData();
    }

    @Override
    protected void onLoadData() {
        loadPage(1);
    }

    private void initAdapter() {
        mDataBind.recycler.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new StatefulBindQuickAdapter<CoinRecord>(R.layout.coin_record_item, null) {
            @Override
            public void loadPageData(int page) {
                loadPage(page);
            }

            @Override
            protected void convert(DataBindViewHolder helper, CoinRecord item) {
                helper.getBinding().setVariable(BR.bean, item);
            }
        };
        mAdapter.bindToRecyclerView(mDataBind.recycler);
    }

    void loadPage(final int page) {
        final Map<String, String> params = new HashMap<>();
        params.put("page", String.valueOf(page));

        if (page == 1) {
            mAdapter.setViewState(MultiStateView.VIEW_STATE_LOADING);
        }
        addSubscribe(RetrofitHelper.getApis().contributionRecord(params)
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                if (respBean.isCodeFail()) {
                    AdapterHelper.solveFailResp(mDataBind.recycler, page, respBean);
                    return;
                }
                List<CoinRecord> data = respBean.getList();
                mAdapter.solvePageData(data, page, respBean.getPageInfo().getCountPage());
            }, RxHelper.getSwitchableErrorConsumer(mAdapter)));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }
}
